package com.dy.web.controller.system;

import com.dy.common.core.controller.BaseController;
import com.dy.common.core.domain.AjaxResult;
import com.dy.common.utils.DateUtils;
import com.dy.system.domain.FreshMedicalMachine;
import com.dy.system.domain.InspectionReport;
import com.dy.system.domain.MedicalInstitution;
import com.dy.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.dy.common.config.DyConfig;
import com.dy.common.utils.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页
 *
 * @author anps
 */
@RestController
public class SysIndexController extends BaseController {
    /** 系统基础配置 */
    @Autowired
    private DyConfig dyConfig;


    @Autowired
    private IFreshMedicalMachineService freshMedicalMachineService;
    @Autowired
    private IMedicalInstitutionService medicalInstitutionService;
    @Autowired
    private IWarningMessageService warningMessageService;
    @Autowired
    private IMedicalEquipmentReportLogService medicalEquipmentReportLogService;
    @Autowired
    private IInspectionReportService inspectionReportService;
    @Autowired
    private ISysDictDataService sysDictDataService;






    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index(){
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", dyConfig.getName(), dyConfig.getVersion());
    }



    @GetMapping("/sys/list")
    public AjaxResult sysIndex(){
        Map<String,Object> resultMap = new HashMap<>();
        String s = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
        String fmt = s +"%02d";
        List<Map<String,Object>> gjList = new ArrayList<>();
        for (int i = 01; i <= 12; i++) {

            String monthValue = String.format(fmt,i);
            Map<String,Object> gjMap = new HashMap<>();
            Integer count = warningMessageService.selectWarningMessageByMonth(monthValue);
            gjMap.put("month",i);
            gjMap.put("gjCount",count);
            gjList.add(gjMap);
        }
        resultMap.put("gjList",gjList);


        List<MedicalInstitution> institutionList = medicalInstitutionService.selectMedicalInstitutionList(new MedicalInstitution());
        long count = institutionList.stream().filter(m -> m.getCreateTime().getTime() >= DateUtils.getStartTimestamp()).count();
        resultMap.put("jgTotal",institutionList.size());
        resultMap.put("dayAddjg",count);
        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        if (CollectionUtils.isEmpty(machineList)){
            resultMap.put("zxsb",0);
            resultMap.put("zxl",0);
            return AjaxResult.success(resultMap);
        }
        long shebei = machineList.stream().filter(m -> "1".equals(m.getNetworkStatus())).count();
        BigDecimal divide = new BigDecimal(shebei).multiply(new BigDecimal(100)).divide(new BigDecimal(machineList.size()), 2, BigDecimal.ROUND_HALF_UP);
        resultMap.put("zxsb",shebei);
        resultMap.put("zxl",divide.toString());
        return AjaxResult.success(resultMap);
    }



    @GetMapping("/sys/workbench/list")
    public AjaxResult workbenchInterface(){
        Map<String,Object> resultMap = new HashMap<>();
        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        resultMap.put("zxsb",machineList.size());
        //日处理量
        Integer zuotianHandle = medicalEquipmentReportLogService.getMedicalHandleData("zuotian",null,null);
        Integer jintianHandle = medicalEquipmentReportLogService.getMedicalHandleData("jintian",null,null);
        Integer fresh = jintianHandle >= zuotianHandle ? jintianHandle - zuotianHandle : 0;
        resultMap.put("dayTotalHandle",fresh);
        //总处理量
        Integer totalHandle = medicalEquipmentReportLogService.getMedicalHandleData(null,null,null);
        resultMap.put("totalHandle",totalHandle);
        return AjaxResult.success(resultMap);
    }


    @GetMapping("/sys/workbench/report")
    public AjaxResult workBenchReport(@RequestParam(required = false) String year){
        MedicalInstitution institution = new MedicalInstitution();
        institution.setCreateBy(getUsername());
        List<MedicalInstitution> institutions = medicalInstitutionService.selectMedicalInstitutionList(institution);
        if (CollectionUtils.isEmpty(institutions)){
            return success();
        }
        MedicalInstitution ins = institutions.get(0);
        Long insId = ins.getId();
        List<InspectionReport> reportList = new ArrayList<>();
        InspectionReport inspectionReport = new InspectionReport();
        inspectionReport.setInstitutionId(insId);
        inspectionReport.setYear(year);
        //查询检测报告
        String img = "https://dyep.cc/prod-api";
        for (int a = 1; a <=4; a ++){
            inspectionReport.setSession(String.valueOf(a));
            InspectionReport report = inspectionReportService.selectInspectionReportDp(inspectionReport);
            if (report == null){
                report = new InspectionReport();
                report.setSession(String.valueOf(a));
                report.setStatus("0");
                report.setDateSection(getSubDateStr(String.valueOf(a)));
            }else{
                report.setDateSection(getSubDateStr(String.valueOf(a)));
                report.setStatus("1");
            }
            report.setReportUrl(StringUtils.isNoneBlank(report.getReportUrl()) ? img + report.getReportUrl() : "");
            String jdName = sysDictDataService.selectDictLabel("report_sc_jd", report.getSession());
            report.setJdName(jdName);
            String dataStr = year;
            if (StringUtils.isBlank(year)){
                dataStr = DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
            }
            report.setYeayName(dataStr);
            report.setYear(dataStr);
            reportList.add(report);
        }
        return AjaxResult.success(reportList);
    }


    public String getSubDateStr(String session){
        String dataFw = null;
        switch (session){
            case "1":
                dataFw = "01月01日~03月31日";
                break;
            case "2":
                dataFw = "04月01日~06月30日";
                break;
            case "3":
                dataFw = "06月01日~09月30日";
                break;
            default:
                dataFw = "10月01日~12月31日";
                break;
        }
        return dataFw;
    }


    public static void main(String[] args) {
        String s = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
        String fmt = s +"%02d";
        for (int i = 1; i <= 12; i++) {
            String monthValue;
            monthValue = String.format(fmt,i);
            //System.out.println(i);
            System.out.println(monthValue);
        }
    }




}
